﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Engine.Entities.Character;

public class SteveNames {

    public const string NAME_UNDEFINED = "noname";

    private LinkedList<string> names_male_free = new LinkedList<string>();
    private LinkedList<string> names_female_free = new LinkedList<string>();

    private LinkedList<string> names_male_busy = new LinkedList<string>();
    private LinkedList<string> names_female_busy = new LinkedList<string>();

    public SteveNames(string[] surnames)
    {
        ShuffleArray(surnames);
        foreach (string row in surnames)
            if( row != "" )
                names_male_free.AddLast(row);
    }

    public SteveNames(string[][] names)
    {
        List<string> tmpMale = new List<string>();
        List<string> tmpFemale = new List<string>();

        foreach (string[] row in names)
        {
            if ( row.Length > 1)
                if (row[1] == "m")
                    tmpMale.Add(row[0]);
                else
                    tmpFemale.Add(row[0]);
        }

        Shuffle(tmpMale);
        Shuffle(tmpFemale);

        names_male_free = new LinkedList<string>(tmpMale);
        names_female_free = new LinkedList<string>(tmpFemale);

    }

    private void Shuffle(IList list)
    {
        System.Random rng = new System.Random();
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            object value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }

    private void ShuffleArray(string[] texts)
    {
        // Knuth shuffle algorithm :: courtesy of Wikipedia :)
        for (int t = 0; t < texts.Length; t++)
        {
            string tmp = texts[t];
            int r = Random.Range(t, texts.Length);
            texts[t] = texts[r];
            texts[r] = tmp;
        }
    }

    internal string GetRandomName(GenderEnum gender)
    {
        string ris = "";
        switch (gender)
        {
            case GenderEnum.Male:
                if ( names_male_free.Count == 0 )
                    names_male_free = new LinkedList<string>(names_male_busy);
                
                ris = names_male_free.First.Value;
                names_male_busy.AddLast(ris);
                names_male_free.RemoveFirst();

                break;
            case GenderEnum.Female:
                if ( names_female_free.Count == 0 )
                    names_female_free = new LinkedList<string>(names_female_busy);

                ris = names_female_free.First.Value;
                names_female_busy.AddLast(ris);
                names_female_free.RemoveFirst();

                break;
        }
        return ris;
    }

    internal string GetRandomSurname()
    {
        if (names_male_free.Count == 0)
            names_male_free = new LinkedList<string>(names_male_busy);

        string ris = names_male_free.First.Value;
        names_male_busy.AddLast(ris);
        names_male_free.RemoveFirst();

        return ris;
    }
}
